// pages/order-bargain/order-bargain.js
var api = require('../../api.js');
var app = getApp();
var access_token = wx.getStorageSync('access_token');
if (access_token == "" || access_token == undefined || access_token == "undefined") {
  wx.navigateTo({
    url: '/pages/login/login',
  })
}
app.getStoreData();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_id :0,
    nickname: "",
    money: 0.00,
    times: 0,
    date:"",
    showModal: false,
    isNow: false,
    kj_bl: 0,
    kj_date: "2018-10-10 00:00:00",
    kj_money: 0.00,
    order_money: 0.00,
    zwsj:true,
    kj_list:null,
    user_info: wx.getStorageSync("user_info"),
    bargain_gz: wx.getStorageSync("bargain_gz"),
  },
  countdown: function () {
    var page = this;
    var appCount = setInterval(function () {

      var hour = parseInt(page.data.times / 3600);
      var min = parseInt((page.data.times % 3600) / 60);
      var sec = page.data.times - (hour * 3600) - (min * 60);
      if (hour < 10) {
        hour = "0" + hour.toString();
      }
      if (min < 10) {
        min = "0" + min.toString();
      }
      if (sec < 10) {
        sec = "0" + sec.toString();
      }
      var date = hour.toString() + ":" + min.toString() + ":" + sec.toString();
      page.setData({
        date: date,
        times: page.data.times-1,
      });
    }, 1000);
  },
  goHome:function(){
    wx.navigateTo({
      url: '/pages/index/index',
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.pageOnLoad(this);
    var page = this;
    var order_id = options.order_id
    var access_token = wx.getStorageSync('access_token');
    if (access_token == "" || access_token == undefined || access_token == "undefined") {
      console.log("order_id1:",order_id)
      console.log("url1:", escape('/pages/order-bargain/order-bargain?order_id=' + order_id.toString()));
      wx.navigateTo({
        url: '/pages/login/login?url=' + escape('/pages/order-bargain/order-bargain?order_id=' + order_id.toString()),
      });
      return
    }
    app.getStoreData();
    app.request({
      url: api.order.bargain_data,
      data: {
        order_id: order_id,
      },
      complete: function () {
        wx.hideLoading();
      },
      success: function (res) {
        console.log(res);
        if (res.code == 0) {
          var hour = parseInt(res.data.times/3600);
          var min = parseInt((res.data.times%3600)/60);
          var sec = res.data.times - (hour*3600) - (min * 60);
          var date = hour.toString() + ":" + min.toString() + ":" + sec.toString();
          page.setData({
            order_id:order_id,
            nickname:res.data.nickname,
            money:res.data.money,
            times:res.data.times,
            bargain_gz: res.data.bargain_gz,
            kj_list:res.data.kj_list,
            zwsj: res.data.zwsj,
            date: date,
            isNow: true,
          });
          page.countdown();
        }
        if (res.code == 1) {
          wx.showToast({
            title: res.msg,
            image: "/images/icon-warning.png",
            duration: 3000,
            success(res){
              /*wx.navigateTo({
                url: '/pages/index/index',
              });*/
              page.setData({
                showModal:false,
              });
            }
          });
        }

      }
    });
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }, 
  submit3232: function (e) {
    console.log(e.detail.formId);
  },
  submit:function(e){
    app.pageOnLoad(this);
    var formId = e.detail.formId;
    console.log("formId:", formId);
    var page = this;
    var order_id = page.data.order_id
    app.request({
      url: api.order.bargain,
      data: {
        order_id: order_id,
        formId: formId,
      },
      complete: function () {
        wx.hideLoading();
      },
      success: function (res) {
        console.log(res);
        if (res.code == 0) {
            page.setData(
              {
                kj_bl: res.data.kj_bl,
                kj_date: res.data.kj_date,
                kj_money: res.data.kj_money,
                order_money: res.data.order_money,
                showModal: true,
              }
            );
            page.onLoad();
        }
        if (res.code == 1) {
          /*wx.showToast({
            title: res.msg,
            image: "/images/icon-warning.png",
            duration: 3000,
            success(res){
              wx.showModal({
                title: '提示',
                content: res.msg,
                success(res) {
                  if (res.confirm) {
                    wx.navigateTo({
                      url: '/pages/index/index'　　// 页面 A
                    });
                  } else if (res.cancel) {
                    wx.navigateTo({
                      url: '/pages/index/index'　　// 页面 A
                    });
                  }
                }
              });
            }
          });*/
          wx.showModal({
            title: '提示',
            content: res.msg,
            success(res) {
              /*if (res.confirm) {
                wx.navigateTo({
                  url: '/pages/index/index'　　// 页面 A
                });
              } else if (res.cancel) {
                wx.navigateTo({
                  url: '/pages/index/index'　　// 页面 A
                });
              }*/
            }
          });
        }
        page.onLoad();
      }
    });
  }, go: function () {
    /*wx.navigateTo({
      url: '/pages/index/index'　　// 页面 A
    })*/
    var page = this;
    page.setData({
      showModal:false,
    })
  }
});